﻿using System;
using System.Collections.Generic;
using System.Text;

namespace ${project}.${app}.Models.Entity.${database}
{
    public class ${table.name}:NRapid.Struct
    {
		${foreach(field in table.fields)}
		${if(field.description!=null)}
		/// <summary>
        /// ${field.description}
        /// </summary>
		${end}
		${if(field.allowNull && field.isValueType)}
		public ${field.csharpType}? ${field.structFieldName};
		${else}
		public ${field.csharpType} ${field.structFieldName};
		${end}
		${end}
		#region 从get列表初始化实体类
		public static bool TryParse(NRapid.NameValueCollection get,out ${table.name} par)
		{
			par=new ${table.name}();
			try
			{
			${foreach(field in table.fields)}
				${if(field.csharpType=="System.Int64")}
					${if(field.allowNull)}
						par.${field.structFieldName}=null;
						if(get["${field.structFieldName}"]!=null)
						{
							par.${field.structFieldName}=System.Int64.Parse(get["${field.structFieldName}"]);
						}
					${else}
						System.Int64.TryParse(get["${field.structFieldName}"],out par.${field.structFieldName});
					${end}
				${elseif(field.csharpType=="System.UInt64")}
					${if(field.allowNull)}
						par.${field.structFieldName}=null;
						if(get["${field.structFieldName}"]!=null)
						{
							par.${field.structFieldName}=System.UInt64.Parse(get["${field.structFieldName}"]);
						}
					${else}
						System.UInt64.TryParse(get["${field.structFieldName}"],out par.${field.structFieldName});
					${end}	
				${elseif(field.csharpType=="System.Int32")}
					${if(field.allowNull)}
						par.${field.structFieldName}=null;
						if(get["${field.structFieldName}"]!=null)
						{
							par.${field.structFieldName}=System.Int32.Parse(get["${field.structFieldName}"]);
						}
					${else}
						System.Int32.TryParse(get["${field.structFieldName}"],out par.${field.structFieldName});
					${end}	
				${elseif(field.csharpType=="System.UInt32")}
					${if(field.allowNull)}
						par.${field.structFieldName}=null;
						if(get["${field.structFieldName}"]!=null)
						{
							par.${field.structFieldName}=System.UInt32.Parse(get["${field.structFieldName}"]);
						}
					${else}
						System.UInt32.TryParse(get["${field.structFieldName}"],out par.${field.structFieldName});
					${end}	
				${elseif(field.csharpType=="System.Int16")}
					${if(field.allowNull)}
						par.${field.structFieldName}=null;
						if(get["${field.structFieldName}"]!=null)
						{
							par.${field.structFieldName}=System.Int16.Parse(get["${field.structFieldName}"]);
						}
					${else}
						System.Int16.TryParse(get["${field.structFieldName}"],out par.${field.structFieldName});
					${end}	
				${elseif(field.csharpType=="System.UInt16")}
					${if(field.allowNull)}
						par.${field.structFieldName}=null;
						if(get["${field.structFieldName}"]!=null)
						{
							par.${field.structFieldName}=System.UInt16.Parse(get["${field.structFieldName}"]);
						}
					${else}
						System.UInt16.TryParse(get["${field.structFieldName}"],out par.${field.structFieldName});
					${end}	
				${elseif(field.csharpType=="System.Byte")}
					${if(field.allowNull)}
						par.${field.structFieldName}=null;
						if(get["${field.structFieldName}"]!=null)
						{
							par.${field.structFieldName}=System.Byte.Parse(get["${field.structFieldName}"]);
						}
					${else}
						System.Byte.TryParse(get["${field.structFieldName}"],out par.${field.structFieldName});
					${end}	
				${elseif(field.csharpType=="System.SByte")}
					${if(field.allowNull)}
						par.${field.structFieldName}=null;
						if(get["${field.structFieldName}"]!=null)
						{
							par.${field.structFieldName}=System.SByte.Parse(get["${field.structFieldName}"]);
						}
					${else}
						System.SByte.TryParse(get["${field.structFieldName}"],out par.${field.structFieldName});
					${end}	
				${elseif(field.csharpType=="System.Single")}
					${if(field.allowNull)}
						par.${field.structFieldName}=null;
						if(get["${field.structFieldName}"]!=null)
						{
							par.${field.structFieldName}=System.Single.Parse(get["${field.structFieldName}"]);
						}
					${else}
						System.Single.TryParse(get["${field.structFieldName}"],out par.${field.structFieldName});
					${end}
				${elseif(field.csharpType=="System.Decimal")}
					${if(field.allowNull)}
						par.${field.structFieldName}=null;
						if(get["${field.structFieldName}"]!=null)
						{
							par.${field.structFieldName}=System.Decimal.Parse(get["${field.structFieldName}"]);
						}
					${else}
						System.Decimal.TryParse(get["${field.structFieldName}"],out par.${field.structFieldName});
					${end}
				${elseif(field.csharpType=="System.Double")}
					${if(field.allowNull)}
						par.${field.structFieldName}=null;
						if(get["${field.structFieldName}"]!=null)
						{
							par.${field.structFieldName}=System.Double.Parse(get["${field.structFieldName}"]);
						}
					${else}
						System.Double.TryParse(get["${field.structFieldName}"],out par.${field.structFieldName});
					${end}
				${elseif(field.csharpType=="System.String")}
						par.${field.structFieldName}=get["${field.structFieldName}"];
				${elseif(field.csharpType=="System.Byte[]")}
				${elseif(field.csharpType=="System.Boolean")}
					${if(field.allowNull)}
						par.${field.structFieldName}=null;
						if(get["${field.structFieldName}"]!=null)
						{
							par.${field.structFieldName}=System.Boolean.Parse(get["${field.structFieldName}"]);
						}
					${else}
						System.Boolean.TryParse(get["${field.structFieldName}"],out par.${field.structFieldName});
					${end}
				${elseif(field.csharpType=="System.DateTime")}
					${if(field.allowNull)}
						par.${field.structFieldName}=null;
						if(get["${field.structFieldName}"]!=null)
						{
							par.${field.structFieldName}=System.DateTime.Parse(get["${field.structFieldName}"]);
						}
					${else}
						System.DateTime.TryParse(get["${field.structFieldName}"],out par.${field.structFieldName});
					${end}
				${elseif(field.csharpType=="System.TimeSpan")}
					${if(field.allowNull)}
						par.${field.structFieldName}=null;
						if(get["${field.structFieldName}"]!=null)
						{
							par.${field.structFieldName}=System.TimeSpan.Parse(get["${field.structFieldName}"]);
						}
					${else}
						System.TimeSpan.TryParse(get["${field.structFieldName}"],out par.${field.structFieldName});
					${end}
				${elseif(field.csharpType=="System.DateTimeOffset")}
					${if(field.allowNull)}
						par.${field.structFieldName}=null;
						if(get["${field.structFieldName}"]!=null)
						{
							par.${field.structFieldName}=System.DateTimeOffset.Parse(get["${field.structFieldName}"]);
						}
					${else}
						System.DateTimeOffset.TryParse(get["${field.structFieldName}"],out par.${field.structFieldName});
					${end}
				${elseif(field.csharpType=="System.Object")}
						par.${field.structFieldName}=get["${field.structFieldName}"];
				${elseif(field.csharpType=="System.Guid")}
					${if(field.allowNull)}
						par.${field.structFieldName}=null;
						if(get["${field.structFieldName}"]!=null)
						{
							par.${field.structFieldName}=System.Guid.Parse(get["${field.structFieldName}"]);
						}
					${else}
						System.Guid.TryParse(get["${field.structFieldName}"],out par.${field.structFieldName});
					${end}
				${elseif(field.csharpType=="Microsoft.SqlServer.Types.SqlGeography")}
				${elseif(field.csharpType=="Microsoft.SqlServer.Types.SqlGeometry")}
				${elseif(field.csharpType=="Microsoft.SqlServer.Types.SqlHierarchyId")}
				${end}
			${end}
			}
			catch
			{
				return false;
			}
			return true;
		}
		#endregion
		#region 写Json字符串
		public override void WriteJson(System.IO.TextWriter tw)
		{
			tw.Write("{");
			${set(i=0)}
			${foreach(field in table.fields)}
				${if(i!=0)}
					tw.Write(",");
				${end}
				${if(field.jsonType=="Number")}
					tw.Write("\"${field.structFieldName}\":");
					${if(field.allowNull)}
					tw.Write(${field.structFieldName}==null?"null" : ${field.structFieldName}.ToString());
					${else}
					tw.Write(${field.structFieldName}.ToString());
					${end}
				${elseif(field.jsonType=="Bool")}
					tw.Write("\"${field.structFieldName}\":");
					${if(field.allowNull)}
					tw.Write(${field.structFieldName}==null?"null":((bool)${field.structFieldName}?"true":"false"));
					${else}
					tw.Write(${field.structFieldName}?"true":"false");
					${end}
				${elseif(field.jsonType=="Time")}
					tw.Write("\"${field.structFieldName}\":");
					tw.Write("\"");
					${if(field.allowNull)}
					tw.Write(${field.structFieldName}==null?"null":${field.structFieldName}.ToString());
					${else}
					tw.Write(${field.structFieldName}.ToString());
					${end}
					tw.Write("\"");
				${elseif(field.jsonType=="String")}
					tw.Write("\"${field.structFieldName}\":");
					tw.Write("\"");
					${if(field.allowNull)}
					tw.Write(${field.structFieldName}==null?"null":${field.structFieldName});
					${else}
					tw.Write(${field.structFieldName});
					${end}
					tw.Write("\"");
				${elseif(field.jsonType=="Object")}
					tw.Write("\"${field.structFieldName}\":");
					tw.Write("\"");
					${if(field.allowNull)}
					tw.Write(${field.structFieldName}==null?"null":${field.structFieldName}.ToString());
					${else}
					tw.Write(${field.structFieldName}.ToString());
					${end}
					tw.Write("\"");
				${elseif(field.jsonType=="Base64")}
					tw.Write("\"${field.structFieldName}\":");
					tw.Write("\"");
					${if(field.allowNull)}
					tw.Write(${field.structFieldName}==null?"null":Convert.ToBase64String(${field.structFieldName}));
					${else}
					tw.Write(Convert.ToBase64String(${field.structFieldName}));
					${end}
					tw.Write("\"");
				${end}
			${set(i=i+1)}
			${end}
			tw.Write("}");
		}
		#endregion
    }
}